const nodemailer = require('nodemailer')
const mail = require('../config/mail')

const transporter = nodemailer.createTransport({
  host: 'smtp.mxhichina.com',
  port: 465,
  secureConnection: true,
  secure: true, // true for 465, false for other ports
  auth: {
    user: mail.user, // generated ethereal user
    pass: mail.password // generated ethereal password
  }
})

exports.e_mail = async(options) => {
  if (!options.from || options.from.indexOf(mail.user) < 0) {
    options.from = '"aranciacheng" <' + mail.user + '>'
  }
  // const mailOptions = {
  //   from: '"aranciacheng" <' + mail.user + '>', // sender address
  //   to: 'aranciacheng@gmail.com', // list of receivers
  //   subject: '默认主题', // Subject line
  //   text: 'scholatsys 向你问好', // plain text body
  //   html: '<b>scholatsys 向你问好</b>' // html body
  // }
  try {
    const info = await transporter.sendMail(options)
    return info.messageId
  } catch (err) {
    console.log(`[FAILED] cannot send e-mail, because ${err}`)
    return -1
  }
}
